<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="icPubKeyOperationVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="AID#"
                       v-bind="formItemLayout">
            <a-input v-decorator="['paraId',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>

          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="根CA公钥索引"
                       v-bind="formItemLayout">
            <a-input v-decorator="['paraIdx',{rules: [{ required: true,pattern: /^.{0,4}$/,max: 4, message: '必填' },
            { pattern: /^.{0,4}$/, message: '请输入长度小于4的字符或数字'}]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="IC卡公钥参数"
                       v-bind="formItemLayout">
            <a-textarea v-decorator="['paraVal',{rules: [{ required: true, message: '必填' },
            { pattern: '^[A-Z0-9]{0,4000}$', message: '请输入大写字母或数字且长度小于4000'}]}]" :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24" v-show="this.isUpd">
          <a-form-item label="审批退回原因"
                       v-bind="formItemLayout">
            <a-textarea v-decorator="['auditRefuseReason',{rules: [{ required: this.isUpd, message: '必填' }]}]" :disabled="true"/>
          </a-form-item>
        </a-col>

      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button" v-show="this.title !== '查看IC卡参数'">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'IcPubKeyOperation',
    props: {
      icPubKeyOperationVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增IC卡参数',
        isStop: false,
        isCancel: false,
        isUpd: false,
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        parentCodeData: [],
        info: {},

      }
    },
    computed: {
      show: {
        get: function () {
          return this.icPubKeyOperationVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {

      reset() {
        this.loading = false
        this.isCancel = false
        this.isUpd = false
        this.isStop = false
        this.title = '新增IC卡参数'
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId

            if (this.title == '新增IC卡参数') {
              this.info.optFlag = 'add'
            } else if (this.title == '修改IC卡参数') {
              this.info.optFlag = 'upd'
            }

            if (this.title == '新增IC卡参数') {
              this.$post('/admin/merParam/icPubKeyInfo/addIcPubKey', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            } else if (this.title == '修改IC卡参数') {
              this.$put('/admin/merParam/icPubKeyInfo/updIcPubKey', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            }
          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['id', 'paraId', 'paraIdx', 'paraVal', 'auditRefuseReason'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setIcPubKeyFormValues({...info}) {
        let fields = ['id', 'paraId', 'paraIdx', 'paraVal', 'auditRefuseReason']
        if (info.operation == 'view') {
          this.isUpd = true
          this.title = '查看IC卡参数'
        } else {
          this.title = '修改IC卡参数'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>




